package com.ctshk.sapp.guide.controller;


import com.alibaba.excel.EasyExcel;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.guide.dto.GuideCollarExcelDTO;
import com.ctshk.rpc.guide.dto.GuideCollarSchedulingExcelDTO;
import com.ctshk.rpc.guide.dto.GuideCollarSchedulingListDTO;
import com.ctshk.rpc.guide.dto.GuideCollarSchedulingQueryDTO;
import com.ctshk.rpc.guide.req.*;
import com.ctshk.rpc.guide.service.IGuideCollarSchedulingService;
import com.ctshk.sapp.guide.convert.LocalDateConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 排班管理 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-03
 */
@Api(value = "排班管理", tags = "排班管理")
@RestController
@RequestMapping("/guide/collar/scheduling")
public class GuideCollarSchedulingController extends BaseController {

    @DubboReference
    private IGuideCollarSchedulingService guideCollarSchedulingService;

    @ApiOperation(value = "新增个人排班", notes = "新增个人排班")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.ADD, operTitle = "新增", operDesc = "新增个人排班")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid GuideCollarSchedulingAddReq req) {
        return guideCollarSchedulingService.add(req, getCurrentUser(getRequest()), false);
    }

    @ApiOperation(value = "查询个人排班", notes = "查询个人排班")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.QUERY, operTitle = "查询", operDesc = "查询个人排班")
    @PostMapping("/query")
    public Result<GuideCollarSchedulingQueryDTO> query(@RequestBody @Valid GuideCollarSchedulingQueryReq req) {
        return Result.success(guideCollarSchedulingService.query(req));
    }

    @ApiOperation(value = "编辑个人排班 弃用", notes = "编辑个人排班 弃用")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.UPDATE, operTitle = "编辑", operDesc = "编辑个人排班")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid GuideCollarSchedulingUpdateReq req) {
        return guideCollarSchedulingService.update(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "查询排班管理列表", notes = "查询排班管理列表")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.QUERY, operTitle = "查询", operDesc = "查询个人排班")
    @PostMapping("/list")
    public Result<PageResponse<GuideCollarSchedulingListDTO>> list(@RequestBody @Valid GuideCollarSchedulingListReq req) {
        return Result.success(guideCollarSchedulingService.list(req));
    }

    @ApiOperation(value = "批量新增个人排班", notes = "新增批量个人排班")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.ADD, operTitle = "新增", operDesc = "批量新增个人排班")
    @PostMapping("/addList")
    public Result add(@RequestBody @Valid GuideCollarSchedulingAddListReq req) {
        return guideCollarSchedulingService.addList(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "批量编辑个人排班", notes = "批量编辑个人排班")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.UPDATE, operTitle = "编辑", operDesc = "批量编辑个人排班")
    @PostMapping("/updateList")
    public Result updateList(@RequestBody @Valid GuideCollarSchedulingUpdateListReq req) {
        return guideCollarSchedulingService.updateList(req, getCurrentUser(getRequest()));
    }

    /**
     * 下载导出
     */
    @PostMapping("/download")
    public void download(@RequestBody @Valid GuideCollarSchedulingListExcelReq req, HttpServletResponse response) throws IOException {
        System.out.println("download");
        List<GuideCollarSchedulingExcelDTO> listDTO = guideCollarSchedulingService.selectListByIds(req);
        System.out.println("service download");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String time = LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String fileName = null;
        if (req.getType() == 1) {
            fileName = "领队排班表" + time;
        } else if (req.getType() == 2) {
            fileName = "导游排班表" + time;
        }
        String sheetName = "排班明细表";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), GuideCollarExcelDTO.class).sheet(sheetName).registerConverter(new LocalDateConverter()).doWrite(listDTO);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @ApiOperation(value = "删除工作安排", notes = "删除工作安排")
    @OperationLogAnnotation(operModul = OperationLogModel.GUIDE, operType = OperationLogType.DELETE)
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        return guideCollarSchedulingService.delete(id, getCurrentUser(getRequest()));
    }
}
